package com.doit.hbase.client;

import com.doit.hbase.utils.HbaseUtils;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Connection;

import java.io.IOException;

/**
 * @DATE 2021/12/13/17:39
 * @Author MDK
 * @Version 2021.2.2
 * 修改表结构
 **/
public class C09_AlterTable {
    public static void main(String[] args) throws Exception {
        Connection conn = HbaseUtils.getConnection();
        Admin admin = HbaseUtils.getAdmin(conn);
        TableName city = TableName.valueOf("tb_city");
        ColumnFamilyDescriptorBuilder info2b = ColumnFamilyDescriptorBuilder.newBuilder("info2".getBytes());

        info2b.setMaxVersions(3);
        ColumnFamilyDescriptor columnFamilyDescriptor = info2b.build();
        admin.modifyColumnFamily(city, columnFamilyDescriptor);

        admin.close();
        conn.close();
    }

    private static void deleteCF(Admin admin, TableName city) throws Exception {
        admin.deleteColumnFamily(city, "info2".getBytes());
    }

    private static void addCF(Admin admin, TableName city) throws Exception {
        ColumnFamilyDescriptorBuilder info2b = ColumnFamilyDescriptorBuilder.newBuilder("info2".getBytes());
        info2b.setMaxVersions(3);
        ColumnFamilyDescriptor columnFamilyDescriptor = info2b.build();
        admin.addColumnFamily(city, columnFamilyDescriptor);
    }
}
